// 从后台获取当前状态
function updateStatus() {
    chrome.runtime.sendMessage({ action: 'getStatus' }, (response) => {
        if (response) {
            // 更新自动接听状态
            document.getElementById('answerStatus').className = 
                response.autoAnswerRunning ? 'status running' : 'status stopped';
            document.getElementById('answerStatusText').textContent = 
                response.autoAnswerRunning ? '运行中' : '未运行';
            document.getElementById('toggleAnswer').textContent = 
                response.autoAnswerRunning ? '停止自动接听' : '开始自动接听';
            document.getElementById('answerInterval').value = response.intervals.answerInterval;
            
            // 更新自动刷新状态
            document.getElementById('refreshStatus').className = 
                response.autoRefreshRunning ? 'status running' : 'status stopped';
            document.getElementById('refreshStatusText').textContent = 
                response.autoRefreshRunning ? '运行中' : '未运行';
            document.getElementById('toggleRefresh').textContent = 
                response.autoRefreshRunning ? '停止自动刷新' : '开始自动刷新';
            document.getElementById('refreshInterval').value = response.intervals.refreshInterval;
            document.getElementById('refreshCounter').textContent = response.autoRefreshCounter;
            
            // 更新网络检测状态
            document.getElementById('networkStatus').className = 
                response.networkCheckRunning ? 'status running' : 'status stopped';
            document.getElementById('networkStatusText').textContent = 
                response.networkCheckRunning ? '运行中' : '未运行';
            document.getElementById('toggleNetwork').textContent = 
                response.networkCheckRunning ? '停止网络检测' : '开始网络检测';
            document.getElementById('networkInterval').value = response.intervals.networkInterval;
            
            // 更新夜间模式状态
            document.getElementById('nightModeStatus').className = 
                response.isNightModeActive ? 'status running' : 'status stopped';
            document.getElementById('nightModeStatusText').textContent = 
                response.isNightModeActive ? '已激活' : '未激活';
            document.getElementById('enableNightMode').checked = response.nightMode.enabled;
            document.getElementById('nightStartHour').value = response.nightMode.startHour;
            document.getElementById('nightEndHour').value = response.nightMode.endHour;
        }
    });
}

// 初始化页面时更新状态
updateStatus();

// 设置定时器定期更新状态
setInterval(updateStatus, 1000);

// 自动接听控制
document.getElementById('toggleAnswer').addEventListener('click', () => {
    chrome.runtime.sendMessage({ action: 'getStatus' }, (response) => {
        if (response.autoAnswerRunning) {
            chrome.runtime.sendMessage({ action: 'stopAutoAnswer' }, updateStatus);
        } else {
            chrome.runtime.sendMessage({ action: 'startAutoAnswer' }, updateStatus);
        }
    });
});

// 自动刷新控制
document.getElementById('toggleRefresh').addEventListener('click', () => {
    chrome.runtime.sendMessage({ action: 'getStatus' }, (response) => {
        if (response.autoRefreshRunning) {
            chrome.runtime.sendMessage({ action: 'stopAutoRefresh' }, updateStatus);
        } else {
            chrome.runtime.sendMessage({ action: 'startAutoRefresh' }, updateStatus);
        }
    });
});

// 网络检测控制
document.getElementById('toggleNetwork').addEventListener('click', () => {
    chrome.runtime.sendMessage({ action: 'getStatus' }, (response) => {
        if (response.networkCheckRunning) {
            chrome.runtime.sendMessage({ action: 'stopNetworkCheck' }, updateStatus);
        } else {
            chrome.runtime.sendMessage({ action: 'startNetworkCheck' }, updateStatus);
        }
    });
});

// 更新间隔设置
document.getElementById('updateAnswerInterval').addEventListener('click', () => {
    const interval = parseInt(document.getElementById('answerInterval').value);
    if (!isNaN(interval) && interval >= 1000) {
        chrome.runtime.sendMessage({
            action: 'updateIntervals',
            intervals: { answerInterval: interval }
        }, updateStatus);
    }
});

document.getElementById('updateRefreshInterval').addEventListener('click', () => {
    const interval = parseInt(document.getElementById('refreshInterval').value);
    if (!isNaN(interval) && interval >= 10000) {
        chrome.runtime.sendMessage({
            action: 'updateIntervals',
            intervals: { refreshInterval: interval }
        }, updateStatus);
    }
});

document.getElementById('updateNetworkInterval').addEventListener('click', () => {
    const interval = parseInt(document.getElementById('networkInterval').value);
    if (!isNaN(interval) && interval >= 1000) {
        chrome.runtime.sendMessage({
            action: 'updateIntervals',
            intervals: { networkInterval: interval }
        }, updateStatus);
    }
});

// 夜间模式设置更新
document.getElementById('updateNightModeSettings').addEventListener('click', () => {
    const startHour = parseInt(document.getElementById('nightStartHour').value);
    const endHour = parseInt(document.getElementById('nightEndHour').value);
    const enabled = document.getElementById('enableNightMode').checked;
    
    if (!isNaN(startHour) && !isNaN(endHour) && 
        startHour >= 0 && startHour <= 23 && 
        endHour >= 0 && endHour <= 23) {
        
        chrome.runtime.sendMessage({
            action: 'updateNightMode',
            nightMode: {
                enabled: enabled,
                startHour: startHour,
                endHour: endHour
            }
        }, updateStatus);
    }
});
    